<?php

namespace FortendBundle\Controller;

use BaseBundle\BaseBundle;
use BaseBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Stopwatch\Section;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Loader\JsonFileLoader;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Translator;
use BaseBundle\Entity\Admin;
use Symfony\Component\Security\Core\Security;

class DefaultController extends Controller
{
    private $rootDir;

    public function __construct(){
        $this->rootDir = dirname(dirname(dirname(dirname(__FILE__))));
    }

    public function indexAction()
    {
        return $this->render('FortendBundle:Default:index.html.twig');
    }

    public function adminAction()
    {
//        dump(hash_algos());exit;
        return new Response('<html><body>Admin page!</body></html>');

        $user = new Admin();
        $plainPassword = '123456';
        $encoder = $this->container->get('security.password_encoder');
        $encoded = $encoder->encodePassword($user, $plainPassword);
        dump($user);
        dump($encoded);
        exit;
//        $user->setPassword($encoded);
    }


    public function loginAction(Request $request)
    {
        $session = $request->getSession();
        if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(
                Security::AUTHENTICATION_ERROR
            );
        } elseif (null !== $session && $session->has(Security::AUTHENTICATION_ERROR)) {
            $error = $session->get(Security::AUTHENTICATION_ERROR);
            $session->remove(Security::AUTHENTICATION_ERROR);
        } else {
            $error = '';
        }

        // last username entered by the user
        $lastUsername = (null === $session) ? '' : $session->get(Security::LAST_USERNAME);

        return $this->render(
            '@Fortend/Default/login.html.twig',
            array(
                // last username entered by the user
                'last_username' => $lastUsername,
                'error'         => $error,
            )
        );
    }


    /**
     * 发送邮件测试
     * @param Request $request
     */
    public function emailAction(Request $request)
    {
        $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom(array('951549518@qq.com'=>'王闰'))
            ->setTo('wangrun@zmit.cn')
            ->setBody('测试发送 时间:'. date('Y-m-d H:i:s'));

        $sent = $this->get('mailer')->send($message);

       die($sent);
    }
}
